<template>
  <!-- 币别管理 -->
  <div class="app-container">
    <FilterForm :data="filterData" @search="getList" labelWidth="86px" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:woodBasicsData:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:woodBasicsData:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:woodBasicsData:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <Table 
      :api="$Api.baseCurrency.list"
      :params="{basicsType:6}"
      :selection="true"
      @select="handleSelectionChange"
      ref="Table"
      height="60vh">
      <el-table-column label="币别代码" align="center" prop="baseCode" />
      <el-table-column label="币别名称" align="center" prop="basicsName" />
      <el-table-column label="是否本币" align="center" key="baseCurrency" >
        <template slot-scope="scope">
          <span>{{ scope.row.baseCurrency == "0" ? '否': '是'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" key="basicsStatus" >
        <template slot-scope="scope">
          <el-switch
          v-model="scope.row.basicsStatus"
          active-value="0"
          inactive-value="1"
          @change="handleStatusChange(scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native.stop="handleUpdate(scope.row)"
            v-hasPermi="['business:woodBasicsData:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.native.stop="handleDelete(scope.row)"
            v-hasPermi="['business:woodBasicsData:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </Table>

    <!-- 添加或修改木业基础数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="币别代码" prop="baseCode">
          <el-input v-model="form.baseCode" placeholder="请输入币别代码" />
        </el-form-item>
        <el-form-item label="币别名称" prop="basicsName">
          <el-input v-model="form.basicsName" placeholder="请输入币别名称" />
        </el-form-item>
        </el-form >
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "WoodBasicsData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      basicsType:'6',
      statusOptions:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        basicsName: [
           { required: true, message: '请输入币别名称', trigger: 'blur' },

        ],
        baseCode: [
            { required: true, message: '请输入币别代码', trigger: 'blur' },
        ],
      },
      filterData: [
        {
          label: "币别",
          name: "baseCurrency",
          type: "none",
        },
        {
          label: "状态",
          name: "basicsStatus",
          type: "select",
          select: {
            option: [],
            selectLabel: "dictLabel",
            selectValue: "dictValue",
            multiple: true,
          },
        },
      ],
    };
  },
  created() {
    this.getDicts("sys_normal_disable").then(res => {
      let { data } = res;
        this.statusOptions = data;
        this.filterData.forEach( (v) =>{
          if( v.name == "basicsStatus") {
              v.select.option = data;
          }
        })
    });
  },
  methods: {
    /** 查询币别列表 */
    getList(params) {
      this.$refs.Table.getData(params);
    },
  
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        baseCode: null,
        basicsName: null,
        baseCurrency: null,
        basicsStatus: "0",
        basicsType: '6',
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.basicsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增币别";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const basicsId = row.basicsId || this.ids
       this.$Api.baseCurrency.detail(basicsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改币别";
      });
    },
    // 币别状态修改
    handleStatusChange(row) {
      let text = row.basicsStatus == "0" ? "启用" : "停用";
       this.$confirm('确认要"' + text + '"此币别吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then( ()=>{
            this.$Api.baseCurrency.update(row)
                .then( (res)=>{
                    let { code } = res;
                    if( code == 200 ) {
                        this.getList();
                        this.msgSuccess(text + "成功");
                    }
                })
        }).catch( ()=>{
              row.basicsStatus = row.basicsStatus === "0" ? "1" : "0";
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.basicsId != null) {
            this.$Api.baseCurrency.update(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.basicsType = this.basicsType
            this.$Api.baseCurrency.add(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const basicsIds = row.basicsId || this.ids;
      this.$confirm('是否确认删除币别编号为"' + basicsIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
        .then( ()=>{
            this.$Api.baseCurrency.del(basicsIds)
                .then( (res)=>{
                    let { code } = res;
                    if( code == 200 ) {
                        this.getList();
                        this.msgSuccess("删除成功");
                    }
                })
        }).catch( ()=>{})
    },
  }
};
</script>
